//
//  ClubVersionView.swift
//  77Club
//
//  Created by Apple on 2024/11/12.
//

import UIKit

class ClubVersionView: UIView {

    private let bgView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("version_colorBg").end()
    private let contentBg = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("version_bg").end()
    private let closeButton = c_make_ex(UIButton.self)
        .image("version_close").end()
    private let cancelButton = c_make_ex(UIButton.self)
        .image("version_cancel").end()
    private let sureButton = c_make_ex(UIButton.self)
        .image("version_sure").end()
    private let bigSureButton = c_make_ex(UIButton.self)
        .image("version_bigSure").end()
    private let contentLabel = c_make_ex(UILabel.self)
        .color("#4B4C4D").font(12.font_medium).line(0).end()
    
    var model: ClubAppUpdateModel?{
        didSet{
            guard let md = model else { return }
            
            // 1. 先处理 Unicode 字符串
            let formattedString = md.update_log.replacingOccurrences(of: "\\u", with: "\\U").replacingOccurrences(of: "\"", with: "\\\"")
            let encodedString = "\"\(formattedString)\""

            if let data = encodedString.data(using: .utf8),
               let decodedString = try? PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? String {
                let finalString = decodedString.replacingOccurrences(of: "<br/>", with: "\n")
                contentLabel.text = finalString
            }

            if md.constraint {
                cancelButton.isHidden = true
                sureButton.isHidden = true
                closeButton.isHidden = true
                bigSureButton.isHidden = false
            }else{
                cancelButton.isHidden = false
                sureButton.isHidden = false
                closeButton.isHidden = false
                bigSureButton.isHidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .black.alpha(0.36)
        
        subviews{
            bgView
            contentBg.subviews{
                cancelButton
                sureButton
                bigSureButton
                contentLabel
            }
            closeButton
        }
        
        bgView.leading(0).width(.screenWidth).height(576.0.Scale).centerVertically()
        contentBg.width(271.0.Scale).height(350.0.Scale).centerHorizontally().centerVertically()
        closeButton.size(36.0.Scale).centerHorizontally()
        closeButton.Top == contentBg.Bottom + 38.0.Scale
        contentLabel.top(185.0.Scale).leading(19.0.Scale).trailing(19.0.Scale)
        cancelButton.leading(19.0.Scale).width(108.0.Scale).height(34.0.Scale).bottom(25.0.Scale)
        sureButton.trailing(20.0.Scale).width(108.0.Scale).height(34.0.Scale).bottom(25.0.Scale)
        bigSureButton.leading(19.0.Scale).trailing(20.0.Scale).height(34.0.Scale).bottom(25.0.Scale)
        
        contentBg.isUserInteractionEnabled = true
        cancelButton.addTarget(self, action: #selector(clickCancelButton), for: .touchUpInside)
        closeButton.addTarget(self, action: #selector(clickCancelButton), for: .touchUpInside)
        sureButton.addTarget(self, action: #selector(clickSureButton), for: .touchUpInside)
        bigSureButton.addTarget(self, action: #selector(clickSureButton), for: .touchUpInside)
    }
    
    @objc private func clickCancelButton(){
        self.removeFromSuperview()
    }
    
    @objc private func clickSureButton(){
        
        if let url = URL(string: "https://apps.apple.com/app/id6695729435"),
           UIApplication.shared.canOpenURL(url){
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
       }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
